package cn.yusei.teamTribe.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {

	/**
	 * 直接输入输出流传递 content 的 http 请求
	 * 
	 * @param requestUrl
	 * @param method
	 * @param content 要写入的内容
	 * @param timeout
	 * @return 请求返回编码为 UTF-8的数据
	 * @throws IOException
	 */
	public static String newRequest(String requestUrl, String method, String content, int timeout) throws IOException {
		URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(method);
        connection.setConnectTimeout(5000);
        connection.connect();
        OutputStream out = connection.getOutputStream();
        out.write(content.getBytes("UTF-8"));
        InputStream in = connection.getInputStream();
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        String line = null;
        while((line = reader.readLine()) != null) {
        	sb.append(line);
        }
        return line;
	}
	
}
